package cn.wetcland.base;

import cn.TCSP;
import cn.util.TCUtil;
import cn.wetcland.pojo.WetclandPlayer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.scoreboard.*;

import java.util.ArrayList;
import java.util.List;

public class WetclandScoreBoard {

    private Scoreboard scoreboard;
    private Objective objective;
    private Player player;
    private boolean isRun;
    /**
     * 用于保存所有的Team
     */
    private Team team;
    private List<Score> scoreList;
    private List<Team> timers;
    private List<Objective> objectiveList;
    private BukkitTask task;

    public WetclandScoreBoard(Player player) {
        timers = new ArrayList<>();
        this.player = player;
        scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
        objective = scoreboard.getObjective(player.getName());
        if (objective == null)
            objective = scoreboard.registerNewObjective(player.getName(), "dummy", "§bWetcland");
        objective.setDisplaySlot(DisplaySlot.SIDEBAR);
        WetclandPlayer one = TCUtil.getAttrPlayer(player.getName());
        // 计分板设置头衔
        String title = one.getTitle() == null ? "" : "  §b[" + one.getTitle() + "]";
        Team team = scoreboard.getTeam(player.getName());
        if (team == null) {
            team = scoreboard.registerNewTeam(player.getName());
        }
        // 对于自己的队伍进行NameTag显示, 而对其他队伍关闭
        team.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.ALWAYS);
        // 对于自己的队伍开启防碰撞体积, 而对其他队伍开启体积碰撞
        team.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.ALWAYS);
        // 设置前缀为等级 后缀为称号 + 会员标识
        team.setPrefix("§d[" + TCUtil.getStringLevel(one.getLevel()) + "]  ");
        String memberTypeString = TCUtil.getMemberTypeString(one.getMemberType());
        if (memberTypeString != null) {
            team.setSuffix(title + " §e[" + memberTypeString + "]");
        } else {
            team.setSuffix(title);
        }
        team.addEntry(player.getName());
    }

    public void startShowing() {
        // 判断是否已经在运行
        if (isRun) {
            return;
        }
        if (player == null || !player.isOnline()) {
            return;
        }
        isRun = true;
        objective.getScore("§eQQ群: §a825200927").setScore(1);
        objective.getScore("§bIP: §amc.wetcland.cn").setScore(2);
        objective.getScore("§d官网: §awetcland.cn").setScore(3);
        /*objective.getScore("§d玩家名: " + player.getName()).setScore(3);
        objective.getScore("§d金币: " + TCSP.getxConomyAPI().getbalance(TCSP.getxConomyAPI().translateUUID(player.getName()))).setScore(2);
*/
        player.setScoreboard(scoreboard);

        task = Bukkit.getScheduler().runTaskTimer(TCSP.getTcsp(), () -> {
            if (!isRun) {
                return;
            }
            WetclandPlayer one = TCUtil.getAttrPlayer(player.getName());
            Team team = scoreboard.getTeam(player.getName());
            String title = one.getTitle() == null ? "" : "  §b[" + one.getTitle() + "]";
            if (team == null) {
                team = scoreboard.registerNewTeam(player.getName());
            }
            team.setPrefix("§d[" + TCUtil.getStringLevel(one.getLevel()) + "]  ");
            String memberTypeString = TCUtil.getMemberTypeString(one.getMemberType());
            if (memberTypeString != null) {
                team.setSuffix(title + " §e[" + memberTypeString + "]");
            } else {
                team.setSuffix(title);
            }
            /*scoreboard.clearSlot(DisplaySlot.SIDEBAR);
            objective.unregister();
            objective = scoreboard.getObjective(player.getName());
            if (objective == null)
                objective = scoreboard.registerNewObjective(player.getName(), "dummy", "§bWetcland");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.getScore("§d玩家名: " + player.getName()).setScore(3);
            objective.getScore("§d金币: " + TCSP.getxConomyAPI().getbalance(TCSP.getxConomyAPI().translateUUID(player.getName()))).setScore(2);
            */
        }, 0L, 20L);
    }

    public void turnOff() {
        isRun = false;
        task.cancel();
    }

}
